# -*- coding: utf-8 -*-

"""
扩充原训练集之后，可以正确识别顺时针写的0了，但是仅限于书写的起点在左侧中间的情况，
当书写的起点在顶部时，就容易被误判成5或6，所以，我不得不，采集一些自己的字迹
加入训练集，教她认认从上面开始写的0.

2016-2-15
"""
import trajectory_preprocess as tra_pre
import time
import os


DIR = r'dataSet\dbrhd'  # data set directory
PREFIX = r'input'  # prefix of filename
SEP = ','  # separator


def preprocess_input(f_name):
    with open(f_name) as f:
        p.load(f.readline())
        f.close()

    return p. export()


def save_input(f_name, vector, digit):
    with open(f_name, 'a') as f:
        for v in vector:
            f.write('{:>3}'.format(str(v)))
            f.write(SEP)
        f.write(str(digit))
        f.write('\n')
        f.close()


p = tra_pre.PreProcessor()

total = 0
f_name = 'sample.txt'
lastModifiedTime = os.path.getmtime(f_name)

print("All ready.")
digit = 0
while True:
    time.sleep(1)

    t = os.path.getmtime(f_name)
    if t > lastModifiedTime:
        # if True:
        lastModifiedTime = t
        total += 1

        if input('get a input') != '':
            print('skip')
            continue
        else:
            print('Input get.')

        my_input = preprocess_input(f_name)[0]
        print(my_input)
        save_input(os.path.join(DIR, '%s_%i.tra' % (PREFIX, digit)), my_input, digit)
        print('total: %i' % total)


